﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

using System.Runtime.InteropServices;

public class Controler : MonoBehaviour {
	public static Controler Instance;
	public DrawLine drawLine;

	public Text LagrangeTimeText;
	public Text NewtomTimeText;
	public Text ThreeNaturalSplinesTimeText;

	private bool LagrangeShow = true;
	private bool NewtonShow = true;
	private bool ThreeNaturalSplinesShow = true;

	double ms;

	long stop_Value = 0;
	long start_Value = 0;
	long freq = 0;

	public delegate void TimerDelegate();//计时委托

	[DllImport("kernel32.dll")]
	extern static short QueryPerformanceCounter(ref long x);
	[DllImport("kernel32.dll")]
	extern static short QueryPerformanceFrequency(ref long x);

	private void Awake() {
		Instance = this;
	}

	private void Start() {
		QueryPerformanceFrequency(ref freq);
		//ChangeRandomPointListButton();
		Invoke("ChangeRandomPointListButton", 0.1f);
	}

	public void ChangeRandomPointListButton() {
		drawLine.pointList = RandomPointList.Instance.CreateRandomPointList();
		drawLine.UpdateLine();
		LagrangeShow = true;
		NewtonShow = true;
		ThreeNaturalSplinesShow = true;
		Lagrange.Instance.drawLine.pointList.Clear();
		Lagrange.Instance.drawLine.UpdateLine();
		Newton.Instance.drawLine.pointList.Clear();
		Newton.Instance.drawLine.UpdateLine();
		ThreeNaturalSplines.Instance.drawLine.pointList.Clear();
		ThreeNaturalSplines.Instance.drawLine.UpdateLine();
	}

	private void Update() {

	}

	//计时方法
	public double MethodTimer(TimerDelegate timerDelegate) {
		QueryPerformanceCounter(ref start_Value); //获取初始前值
		timerDelegate();//执行委托函数
		QueryPerformanceCounter(ref stop_Value);//获取终止变量值
		//CPU时序转ms
		double ms = (stop_Value - start_Value) / (double)freq * 1000;
		return ms;
	}

	public void LagrangeButton() {
		if (LagrangeShow) {
			ms = MethodTimer(Lagrange.Instance.LagrangeAlgorithm);
			LagrangeTimeText.text = ms.ToString() + "ms";
			Lagrange.Instance.drawLine.UpdateLine();
			Lagrange.Instance.GetComponent<Transform>().transform.position = new Vector3(0, 0, -0.1f);
			Newton.Instance.GetComponent<Transform>().transform.position = new Vector3(0, 0, 0);
		}
		else {
			LagrangeTimeText.text = "";
			Lagrange.Instance.drawLine.pointList.Clear();
			Lagrange.Instance.drawLine.UpdateLine();
		}
		LagrangeShow = !LagrangeShow;
	}

	public void NewtonButton() {
		if (NewtonShow) {
			ms = MethodTimer(Newton.Instance.NewtonAlgorithm);
			NewtomTimeText.text = ms.ToString() + "ms";
			Newton.Instance.drawLine.UpdateLine();
			Newton.Instance.GetComponent<Transform>().transform.position = new Vector3(0, 0, -0.1f);
			Lagrange.Instance.GetComponent<Transform>().transform.position = new Vector3(0, 0, 0);
		}
		else {
			NewtomTimeText.text = "";
			Newton.Instance.drawLine.pointList.Clear();
			Newton.Instance.drawLine.UpdateLine();
		}
		NewtonShow = !NewtonShow;
	}

	public void ThreeNaturalSplinesButton() {
		if (ThreeNaturalSplinesShow) {
			ms = MethodTimer(ThreeNaturalSplines.Instance.ThreeNaturalSplinesAlgorithm);
			ThreeNaturalSplinesTimeText.text = ms.ToString() + "ms";
			ThreeNaturalSplines.Instance.drawLine.UpdateLine();
		}
		else {
			ThreeNaturalSplinesTimeText.text = "";
			ThreeNaturalSplines.Instance.drawLine.pointList.Clear();
			ThreeNaturalSplines.Instance.drawLine.UpdateLine();
		}
		ThreeNaturalSplinesShow = !ThreeNaturalSplinesShow;
	}



}
